<?php

use Dotenv\Dotenv;

if (!function_exists('env')) {
    function env($key, $default = null)
    {
        static $cache = [];

        if (isset($cache[$key])) {
            return $cache[$key];
        }

        // Swoole 环境下优先从 $_ENV 读取
        $value = $_ENV[$key] ?? $_SERVER[$key] ?? getenv($key);

        if ($value === false) {
            return $default;
        }

        // 类型转换逻辑...

        // 缓存结果（重要！避免重复解析）
        return $cache[$key] = $value;
    }
}
/**
 * 路径辅助函数
 */

if (!function_exists('base_path')) {
    function base_path(string $path = ''): string
    {
        return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . ($path ? DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }
}

if (!function_exists('app_path')) {
    function app_path(string $path = ''): string
    {
        return base_path('app') . ($path ? DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }
}
if (!function_exists('public_path')) {
    function public_path(string $path = ''): string
    {
        return base_path('public') . ($path ? DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }
}

if (!function_exists('storage_path')) {
    function storage_path(string $path = ''): string
    {
        return base_path('storage') . ($path ? DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }
}

if (!function_exists('config_path')) {
    function config_path(string $path = ''): string
    {
        return base_path('config') . ($path ? DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }
}

if (!function_exists('resource_path')) {
    function resource_path(string $path = ''): string
    {
        return base_path('resources') . ($path ? DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }
}

if (!function_exists('config')) {
    /**
     * 获取配置值
     *
     * @param string $key 配置键名，支持点语法和路径分隔符，"app.name",或 "apps/http.middlewares"
     * @param mixed $default 默认值
     * @return mixed
     */
    function config(string $key, $default = null)
    {
        static $configurations = [];

        // 解析键名，支持两种格式：
        // 1. config('app.name') - 点语法
        // 2. config('apps/http.middlewares') - 路径语法
        $keyParts = strpos($key, '/') !== false
            ? explode('/', $key)
            : explode('.', $key);

        $fileKey = array_shift($keyParts);
        $filePath = config_path("{$fileKey}.php");
        // 如果配置未加载，则加载配置文件
        if (!isset($configurations[$fileKey])) {
            if (!file_exists($filePath)) {
                return $default;
            }

            $configurations[$fileKey] = require $filePath;
        }

        // 遍历获取配置值
        $value = $configurations[$fileKey];
        foreach ($keyParts as $part) {
            if (!is_array($value) || !array_key_exists($part, $value)) {
                return $default;
            }
            $value = $value[$part];
        }

        return $value;
    }
}
